package com.iphone.extra.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.SparseIntArray;
import android.view.View;

import com.iphone.extra.R;

public class ScreenTimeView extends View {

    private final Paint mTextPaint;
    private final Paint mLinePaint;
    private final Paint mDotLinePaint;
    private final Paint mColumnPaint;

    private final Point lineStart = new Point();
    private final Point lineEnd = new Point();
    private final Point textStart = new Point();
    private final RectF rect = new RectF();
    private final Rect TEXT_RECT = new Rect();
    private final int[] mTimeList = new int[24];

    private final String MINUTE_60;
    private final String MINUTE_30;
    private final String MINUTE_0;
    private final String START_TIME;

    private String mStartTime = "";

    private final int TextSize;
    private final int LineSize;
    private final int colLeftCount;
    private final int colRightCount;

    public ScreenTimeView(Context context) {
        this(context, null, 0);
    }

    public ScreenTimeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScreenTimeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Resources res = context.getResources();

        TextSize = res.getDimensionPixelSize(R.dimen.screen_time_text_size);
        LineSize = res.getDimensionPixelSize(R.dimen.screen_time_line_size);

        MINUTE_60 = "60" + res.getString(R.string.screen_time_minute);
        MINUTE_30 = "30" + res.getString(R.string.screen_time_minute);
        MINUTE_0 = "0" + res.getString(R.string.screen_time_minute);
        START_TIME = res.getString(R.string.screen_time_simple_hour);

        mTextPaint = new Paint();
        mTextPaint.setColor(0xff8a8a8a);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setDither(true);
        mTextPaint.setTextSize(TextSize);
        mTextPaint.setTextAlign(Paint.Align.LEFT);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.getTextBounds(MINUTE_60, 0, MINUTE_60.length(), TEXT_RECT);

        mColumnPaint = new Paint(mTextPaint);
        mColumnPaint.setColor(0xff3478f6);

        mLinePaint = new Paint();
        mLinePaint.setColor(0xffe1e1e2);
        mLinePaint.setStrokeWidth(LineSize - 1);

        mDotLinePaint = new Paint(mLinePaint);
        mDotLinePaint.setPathEffect(new DashPathEffect(new float[]{LineSize + 1, LineSize + 1}, 0));

        if ("4".equals(getTag())) {
            colLeftCount = 10;
            colRightCount = 4;
        } else {
            colLeftCount = 6;
            colRightCount = 2;
        }
    }

    public void updateScreenTime(int hour, SparseIntArray array) {
        int m = 0;
        for (int i = (colLeftCount + colRightCount - 1); i >= 0; i--) {
            if (hour - m >= 0) {
                mTimeList[i] = array.get(hour - m);
            } else {
                mTimeList[i] = 0;
            }
            m++;
        }
        if (hour >= (colLeftCount + colRightCount)) {
            mStartTime = (hour - (colLeftCount + colRightCount)) + START_TIME;
        } else {
            mStartTime = (24 - ((colLeftCount + colRightCount) - hour)) + START_TIME;
        }
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        int left = getPaddingLeft();
        int top = getPaddingTop();
        int right = getPaddingRight();
        int bottom = getPaddingBottom();

        int width = getWidth() - left - right;
        int height = getHeight() - top - bottom;
        int gap = (getHeight() - TEXT_RECT.height() * 4) / 2;

        textStart.y = top + TEXT_RECT.height();
        lineStart.x = left;
        lineStart.y = top + textStart.y - TEXT_RECT.height() / 2;
        lineEnd.x = left + width - TEXT_RECT.width();
        textStart.x = lineEnd.x;

        int colW = (lineEnd.x - lineStart.x) / ((colLeftCount + colRightCount) * 3);
        int dotLineY = lineStart.y;

        canvas.drawLine(lineStart.x, lineStart.y, lineEnd.x, lineStart.y, mLinePaint);
        canvas.drawText(MINUTE_60, textStart.x, textStart.y, mTextPaint);

        lineStart.y += TEXT_RECT.height() + gap;
        textStart.y += TEXT_RECT.height() + gap;
        canvas.drawLine(lineStart.x, lineStart.y, lineEnd.x, lineStart.y, mLinePaint);
        canvas.drawText(MINUTE_30, textStart.x, textStart.y, mTextPaint);

        lineStart.y += TEXT_RECT.height() + gap;
        textStart.y += TEXT_RECT.height() + gap;
        canvas.drawLine(lineStart.x, lineStart.y, lineEnd.x, lineStart.y, mLinePaint);
        canvas.drawText(MINUTE_0, textStart.x, textStart.y, mTextPaint);
        canvas.drawText(mStartTime, lineStart.x + 4, lineStart.y + TEXT_RECT.height(), mTextPaint);
        canvas.drawLine(lineStart.x, dotLineY, lineStart.x, dotLineY + height, mDotLinePaint);

        lineStart.x += LineSize;
        for (int i = 0; i < colLeftCount; i++) {
            rect.left = lineStart.x + (colW * 3) * i;
            rect.right = rect.left + colW * 2;
            rect.top = lineStart.y - mTimeList[i] * (lineStart.y - dotLineY) / 60.0f;
            rect.bottom = lineStart.y;
            canvas.drawRoundRect(rect, LineSize, LineSize, mColumnPaint);
        }

        lineStart.x = (int) (rect.right + 4);
        canvas.drawLine(lineStart.x, dotLineY, lineStart.x, dotLineY + height, mDotLinePaint);
        lineStart.x += LineSize;

        for (int i = colLeftCount; i < colLeftCount + colRightCount; i++) {
            rect.left = lineStart.x + (colW * 3) * (i - colLeftCount);
            rect.right = rect.left + colW * 2;
            rect.top = lineStart.y - mTimeList[i] * (lineStart.y - dotLineY) / 60.0f;
            rect.bottom = lineStart.y;
            canvas.drawRoundRect(rect, LineSize, LineSize, mColumnPaint);
        }
    }
}
